<?php
$form = $this->beginWidget('CActiveForm', array(
    'enableAjaxValidation' => false,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
?> 

<?php // echo $form->errorSummary($model); ?>

<?php foreach ($fields as $field => $params) { ?>

    <?php $titleField = !empty($params['title']) ? $params['title'] : $form->label($model, $field); ?>
    <div class="row-form">
        <div class="span2"><?= strip_tags($titleField); ?></div>

        <div class="span10">
            <?php $htmlOptions = !empty($params['htmlOptions']) ? $params['htmlOptions'] : array(); ?>

            <?php // если textField, passwordField, textArea ?>
            <?php if (($params['type'] == 'textField') || ($params['type'] == 'passwordField') || ($params['type'] == 'textArea')) { ?>
                <?php echo $form->{$params['type']}($model, $field, $htmlOptions) ?>
            <?php } ?>

            <?php // если dropDownList ?>
            <?php if ($params['type'] == 'dropDownList') { ?>
                <?php
                    $data = json_decode($model->{$field}, 1);
                    
                    $options = array();
                    if(is_array($data)){
                        foreach($data as $item){
                            $options[$item] = array('selected'=>true);
                        }
                    } else {
                        $options[$data] = array('selected'=>true);
                    }
                    
                    $htmlOptions['options'] = $options;
                ?>
            
                <?php $data = !empty($params['data']) ? $params['data'] : array('' => 'Список пуст'); ?>
                <?php echo $form->{$params['type']}($model, $field, $data, $htmlOptions) ?>
                
            <?php } ?>

            <?php // если ckeditor  ?>
            <?php if ($params['type'] == 'ckeditor') { ?>
                <?php echo $form->textArea($model, $field, $htmlOptions) ?>
                <script>
                    $(document).ready(function() {
                        var ckeditor = CKEDITOR.replace('<?= get_class($model) . "[" . $field . "]"; ?>');
                        $("#submitEditor1").click(function() {
                            alert(ckeditor.getData());
                        });
                    });
                </script> 
            <?php } ?>

            <?php // если imageUpload   ?>
            <?php if ($params['type'] == 'imageUpload') { ?>
                <div class="fileupload fileupload-new" data-provides="fileupload">
                    <div class="fileupload-preview thumbnail" style="width: <?= $params['htmlOptions']['width']; ?>px; height: <?= $params['htmlOptions']['height']; ?>px;">
                        <?php if (!empty($model->{$field})) { ?>
                            <img src="<?= $params['path'] . $model->id . "/" . $model->{$field}; ?>"/>
                        <?php } else { ?>
                            <img src="/themes/virgo/img/noimg_s.gif"/>
                        <?php } ?>
                    </div>
                    <div>
                        <span class="btn btn-file btn-primary">
                            <span class="fileupload-new">Select image</span>
                            <span class="fileupload-exists">Change</span>
                            <?php echo CHtml::activeFileField($model, $field); ?>
                        </span>
                        <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
                    </div>
                </div>
            <?php } ?>
                
            <?php // если checkbox   ?>
            <?php if ($params['type'] == 'checkbox') { ?>
                <?php echo $form->{$params['type']}($model, $field, $htmlOptions) ." ".$params['desription']; ?>
            <?php } ?>

            <span class="bottom"><?php echo $form->error($model, $field); ?></span>
        </div>
    </div>


<?php } ?>


<div class="toolbar bottom TAL">
    <button class="btn btn-primary">Submit</button>
</div>

<?php $this->endWidget(); ?>